package cn.iocoder.yudao.module.system.service.WeChatWorkInterface;

import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO;
import cn.iocoder.yudao.module.infra.service.config.ConfigService;
import cn.iocoder.yudao.module.system.controller.admin.weChatTextCardMsgSend.ObjectFactory;
import cn.iocoder.yudao.module.system.controller.admin.weChatTextCardMsgSend.TextCardMsgSend;
import cn.iocoder.yudao.module.system.controller.admin.weChatTextCardMsgSend.WeChatWorkInterface1;
import cn.iocoder.yudao.module.system.controller.admin.weChatTextCardMsgSend.WeChatWorkInterface1Soap;
import cn.iocoder.yudao.module.system.dal.dataobject.seatreserve.SeatReserveDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.format.DateTimeFormatter;

@Service
@Validated
@Slf4j
public class TextCardMsgSendServiceImpl implements TextCardMsgSendService{

    @Resource
    private ConfigService configService;
    @Override
    public void TextCardMsgSend(String userCode, SeatReserveDO seatReserve,String template) {
        try {
            ConfigDO config = configService.getConfigByKey("textCardMsgSend-"+template);
            if(config!=null) {
                /*获取应用id和消息发送模板*/
                /*应用id*/
                String agentid = config.getValue();
                /*消息标题*/
                String title = config.getName();
                String contentAndUrl = config.getRemark();
                String [] info  =contentAndUrl.split("@#@");
                String templateInfo = info[0];
                String SSOUrl = info[1];
                //日期类型格式化样式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                // 创建ObjectFactory实例
                ObjectFactory objectFactory = new ObjectFactory();
                // 使用ObjectFactory创建TextCardMsgSend请求对象并设置参数
                TextCardMsgSend textCardMsgSendRequest = objectFactory.createTextCardMsgSend();
                textCardMsgSendRequest.setTouser(userCode); // 请替换为实际的用户ID
                textCardMsgSendRequest.setTitle(title); // 请替换为实际的标题
                textCardMsgSendRequest.setUrl(SSOUrl); // 请替换为实际的URL
                textCardMsgSendRequest.setAgentid(agentid); // 请替换为实际的应用ID
                if(template.equalsIgnoreCase("cancel")) {//根据取消模板赋值
                    textCardMsgSendRequest.setDesc(String.format(templateInfo,
                            seatReserve.getReserverUsername(),
                            seatReserve.getReserveDate().format(formatter),
                            seatReserve.getReserveTime().equals("1")?"上午":"下午",
                            seatReserve.getSeatName())); // 请替换为实际的描述

                }else{
                    textCardMsgSendRequest.setTitle("预定成功"); // 请替换为实际的标题
                    textCardMsgSendRequest.setDesc(String.format(templateInfo,
                            seatReserve.getReserverUsername(),
                            seatReserve.getReserveDate().format(formatter),
                            seatReserve.getReserveTime().equals("1")?"上午":"下午",
                            seatReserve.getSeatName())); // 请替换为实际的描述
                }

                // 获取WSDL文件的URL
                String wsdlLocation = "file:/D:/code/src/main/java/WeChatWorkInterfaceforTextCardMsgSend.wsdl";
                URL url = new URL(wsdlLocation);

                // 创建WeChatWorkInterface1服务实例
                javax.xml.ws.Service service = new WeChatWorkInterface1(url);

                // 获取WeChatWorkInterface1Soap端口实例
                cn.iocoder.yudao.module.system.controller.admin.weChatTextCardMsgSend.WeChatWorkInterface1Soap port = service.getPort(WeChatWorkInterface1Soap.class);

                // 调用textCardMsgSend方法并获取响应
                String response = port.textCardMsgSend(
                        textCardMsgSendRequest.getTouser(),
                        textCardMsgSendRequest.getTitle(),
                        textCardMsgSendRequest.getDesc(),
                        textCardMsgSendRequest.getUrl(),
                        textCardMsgSendRequest.getAgentid());

                // 输出响应结果
                log.info("发送消息的响应结果：" + response);
            }
        } catch (WebServiceException | MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
